{
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# t-distributed Stochastic Neighbor Embedding (t-SNE)"
      ],
      "metadata": {}
    },
    {
      "cell_type": "markdown",
      "source": [
        "t-SNE is stochastic algorithm and is non-linear. It is local and global approach; however, it has no truncation of dimensions and no solution."
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "import numpy as np\n",
        "import matplotlib.pyplot as plt\n",
        "import pandas as pd\n",
        "\n",
        "import warnings\n",
        "warnings.filterwarnings(\"ignore\")\n",
        "\n",
        "# yahoo finance is used to fetch data \n",
        "import yfinance as yf\n",
        "yf.pdr_override()"
      ],
      "outputs": [],
      "execution_count": 1,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-27T00:31:23.049Z",
          "iopub.execute_input": "2020-07-27T00:31:23.054Z",
          "iopub.status.idle": "2020-07-27T00:31:24.055Z",
          "shell.execute_reply": "2020-07-27T00:31:24.087Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# input\n",
        "symbol = 'AMD'\n",
        "start = '2014-01-01'\n",
        "end = '2018-08-27'\n",
        "\n",
        "# Read data \n",
        "dataset = yf.download(symbol,start,end)\n",
        "\n",
        "# View Columns\n",
        "dataset.head()"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[*********************100%***********************]  1 of 1 completed\n"
          ]
        },
        {
          "output_type": "execute_result",
          "execution_count": 2,
          "data": {
            "text/plain": "            Adj Close  Close  High   Low  Open    Volume\nDate                                                    \n2014-01-02       3.95   3.95  3.98  3.84  3.85  20548400\n2014-01-03       4.00   4.00  4.00  3.88  3.98  22887200\n2014-01-06       4.13   4.13  4.18  3.99  4.01  42398300\n2014-01-07       4.18   4.18  4.25  4.11  4.19  42932100\n2014-01-08       4.18   4.18  4.26  4.14  4.23  30678700",
            "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Adj Close</th>\n      <th>Close</th>\n      <th>High</th>\n      <th>Low</th>\n      <th>Open</th>\n      <th>Volume</th>\n    </tr>\n    <tr>\n      <th>Date</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2014-01-02</th>\n      <td>3.95</td>\n      <td>3.95</td>\n      <td>3.98</td>\n      <td>3.84</td>\n      <td>3.85</td>\n      <td>20548400</td>\n    </tr>\n    <tr>\n      <th>2014-01-03</th>\n      <td>4.00</td>\n      <td>4.00</td>\n      <td>4.00</td>\n      <td>3.88</td>\n      <td>3.98</td>\n      <td>22887200</td>\n    </tr>\n    <tr>\n      <th>2014-01-06</th>\n      <td>4.13</td>\n      <td>4.13</td>\n      <td>4.18</td>\n      <td>3.99</td>\n      <td>4.01</td>\n      <td>42398300</td>\n    </tr>\n    <tr>\n      <th>2014-01-07</th>\n      <td>4.18</td>\n      <td>4.18</td>\n      <td>4.25</td>\n      <td>4.11</td>\n      <td>4.19</td>\n      <td>42932100</td>\n    </tr>\n    <tr>\n      <th>2014-01-08</th>\n      <td>4.18</td>\n      <td>4.18</td>\n      <td>4.26</td>\n      <td>4.14</td>\n      <td>4.23</td>\n      <td>30678700</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
          },
          "metadata": {}
        }
      ],
      "execution_count": 2,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-27T00:31:24.066Z",
          "iopub.execute_input": "2020-07-27T00:31:24.072Z",
          "iopub.status.idle": "2020-07-27T00:31:25.421Z",
          "shell.execute_reply": "2020-07-27T00:31:25.609Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "dataset['Increase_Decrease'] = np.where(dataset['Volume'].shift(-1) > dataset['Volume'],1,0)\n",
        "dataset['Buy_Sell_on_Open'] = np.where(dataset['Open'].shift(-1) > dataset['Open'],1,0)\n",
        "dataset['Buy_Sell'] = np.where(dataset['Adj Close'].shift(-1) > dataset['Adj Close'],1,0)\n",
        "dataset['Returns'] = dataset['Adj Close'].pct_change()\n",
        "dataset = dataset.dropna()"
      ],
      "outputs": [],
      "execution_count": 3,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2020-07-27T00:31:25.435Z",
          "iopub.execute_input": "2020-07-27T00:31:25.443Z",
          "iopub.status.idle": "2020-07-27T00:31:25.457Z",
          "shell.execute_reply": "2020-07-27T00:31:25.615Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "dataset.tail()"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 4,
          "data": {
            "text/plain": "            Adj Close      Close   High        Low       Open     Volume  \\\nDate                                                                       \n2018-08-20  19.980000  19.980000  20.08  19.350000  19.790001   62983200   \n2018-08-21  20.400000  20.400000  20.42  19.860001  19.980000   55629000   \n2018-08-22  20.900000  20.900000  20.92  20.209999  20.280001   62002700   \n2018-08-23  22.290001  22.290001  22.32  21.139999  21.190001  113444100   \n2018-08-24  23.980000  23.980000  24.00  22.670000  22.910000  164328200   \n\n            Increase_Decrease  Buy_Sell_on_Open  Buy_Sell   Returns  \nDate                                                                 \n2018-08-20                  0                 1         1  0.010622  \n2018-08-21                  1                 1         1  0.021021  \n2018-08-22                  1                 1         1  0.024510  \n2018-08-23                  1                 1         1  0.066507  \n2018-08-24                  0                 0         0  0.075819  ",
            "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Adj Close</th>\n      <th>Close</th>\n      <th>High</th>\n      <th>Low</th>\n      <th>Open</th>\n      <th>Volume</th>\n      <th>Increase_Decrease</th>\n      <th>Buy_Sell_on_Open</th>\n      <th>Buy_Sell</th>\n      <th>Returns</th>\n    </tr>\n    <tr>\n      <th>Date</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2018-08-20</th>\n      <td>19.980000</td>\n      <td>19.980000</td>\n      <td>20.08</td>\n      <td>19.350000</td>\n      <td>19.790001</td>\n      <td>62983200</td>\n      <td>0</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0.010622</td>\n    </tr>\n    <tr>\n      <th>2018-08-21</th>\n      <td>20.400000</td>\n      <td>20.400000</td>\n      <td>20.42</td>\n      <td>19.860001</td>\n      <td>19.980000</td>\n      <td>55629000</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0.021021</td>\n    </tr>\n    <tr>\n      <th>2018-08-22</th>\n      <td>20.900000</td>\n      <td>20.900000</td>\n      <td>20.92</td>\n      <td>20.209999</td>\n      <td>20.280001</td>\n      <td>62002700</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0.024510</td>\n    </tr>\n    <tr>\n      <th>2018-08-23</th>\n      <td>22.290001</td>\n      <td>22.290001</td>\n      <td>22.32</td>\n      <td>21.139999</td>\n      <td>21.190001</td>\n      <td>113444100</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0.066507</td>\n    </tr>\n    <tr>\n      <th>2018-08-24</th>\n      <td>23.980000</td>\n      <td>23.980000</td>\n      <td>24.00</td>\n      <td>22.670000</td>\n      <td>22.910000</td>\n      <td>164328200</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0.075819</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
          },
          "metadata": {}
        }
      ],
      "execution_count": 4,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2020-07-27T00:31:25.468Z",
          "iopub.execute_input": "2020-07-27T00:31:25.475Z",
          "iopub.status.idle": "2020-07-27T00:31:25.500Z",
          "shell.execute_reply": "2020-07-27T00:31:26.263Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "dataset.shape"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 5,
          "data": {
            "text/plain": "(1170, 10)"
          },
          "metadata": {}
        }
      ],
      "execution_count": 5,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-27T00:31:25.513Z",
          "iopub.execute_input": "2020-07-27T00:31:25.519Z",
          "iopub.status.idle": "2020-07-27T00:31:25.536Z",
          "shell.execute_reply": "2020-07-27T00:31:26.268Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "X = np.array(dataset)"
      ],
      "outputs": [],
      "execution_count": 6,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-27T00:31:25.549Z",
          "iopub.execute_input": "2020-07-27T00:31:25.554Z",
          "iopub.status.idle": "2020-07-27T00:31:25.566Z",
          "shell.execute_reply": "2020-07-27T00:31:26.271Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Import TSNE\n",
        "from sklearn.manifold import TSNE"
      ],
      "outputs": [],
      "execution_count": 7,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2020-07-27T00:31:25.579Z",
          "iopub.execute_input": "2020-07-27T00:31:25.584Z",
          "iopub.status.idle": "2020-07-27T00:31:26.203Z",
          "shell.execute_reply": "2020-07-27T00:31:26.275Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "model = TSNE(learning_rate=200)"
      ],
      "outputs": [],
      "execution_count": 8,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-27T00:31:26.214Z",
          "iopub.execute_input": "2020-07-27T00:31:26.219Z",
          "iopub.status.idle": "2020-07-27T00:31:26.234Z",
          "shell.execute_reply": "2020-07-27T00:31:26.279Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "model.fit_transform(X)"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 9,
          "data": {
            "text/plain": "array([[-34.264164 ,  12.402311 ],\n       [ 11.855803 ,  17.418474 ],\n       [ 11.961982 ,  15.607307 ],\n       ...,\n       [-11.343261 ,   7.9292912],\n       [ 45.217545 ,   3.628853 ],\n       [ 49.437145 ,  -0.8107653]], dtype=float32)"
          },
          "metadata": {}
        }
      ],
      "execution_count": 9,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-27T00:31:26.244Z",
          "iopub.execute_input": "2020-07-27T00:31:26.251Z",
          "iopub.status.idle": "2020-07-27T00:31:32.603Z",
          "shell.execute_reply": "2020-07-27T00:31:45.014Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "X_embedded = TSNE(n_components=2).fit_transform(X)\n",
        "X_embedded.shape\n"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 10,
          "data": {
            "text/plain": "(1170, 2)"
          },
          "metadata": {}
        }
      ],
      "execution_count": 10,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2020-07-27T00:31:32.614Z",
          "iopub.execute_input": "2020-07-27T00:31:32.622Z",
          "iopub.status.idle": "2020-07-27T00:31:38.898Z",
          "shell.execute_reply": "2020-07-27T00:31:45.018Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import time\n",
        "\n",
        "time_start = time.time()\n",
        "\n",
        "time_tsne = TSNE(random_state=123).fit_transform(X)\n",
        "\n",
        "print('t-SNE done! Time elapsed: {} seconds'.format(time.time()-time_start))"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "t-SNE done! Time elapsed: 6.093719720840454 seconds\n"
          ]
        }
      ],
      "execution_count": 11,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2020-07-27T00:31:38.908Z",
          "iopub.execute_input": "2020-07-27T00:31:38.915Z",
          "iopub.status.idle": "2020-07-27T00:31:45.002Z",
          "shell.execute_reply": "2020-07-27T00:31:45.021Z"
        }
      }
    }
  ],
  "metadata": {
    "kernel_info": {
      "name": "python3"
    },
    "language_info": {
      "mimetype": "text/x-python",
      "version": "3.5.5",
      "nbconvert_exporter": "python",
      "file_extension": ".py",
      "name": "python",
      "pygments_lexer": "ipython3",
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      }
    },
    "kernelspec": {
      "name": "python3",
      "language": "python",
      "display_name": "Python 3"
    },
    "nteract": {
      "version": "0.24.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 4
}